/*
 * Copyright (c) 2015. Visionet and/or its affiliates. All right reserved.
 * VISIONET PROPRIETARY/CONFIDENTIAL.
 */

package com.visionet.jumper.project.sample.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.visionet.jumper.core.domain.bean.Page;
import com.visionet.jumper.core.domain.bean.QueryBean;
import com.visionet.jumper.core.domain.search.HqlSearchable;
import com.visionet.jumper.core.domain.search.Searchable;
import com.visionet.jumper.core.domain.search.SqlSearchable;
import com.visionet.jumper.core.domain.search.enumdata.SearchOperator;
import com.visionet.jumper.core.domain.search.filter.OrCondition;
import com.visionet.jumper.core.repository.BaseRepository;
import com.visionet.jumper.core.service.impl.AbstractBaseService;
import com.visionet.jumper.core.support.jpa.SimpleBaseRepository;
import com.visionet.jumper.project.sample.domain.entity.Sample;
import com.visionet.jumper.project.sample.repository.SampleRepository;
import com.visionet.jumper.project.sample.service.SampleService;
import com.visionet.jumper.project.sample.vo.SampleVo;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @author Sin
 * @date 11/29/2015.
 */
@Service
public class SampleServiceImpl extends AbstractBaseService<Sample> implements SampleService {

	@Resource
	private SampleRepository sampleRepository;
	
	@Override
	public void saveSample(SampleVo sampleVo) {
		System.out.println("进行保存方法操作----");
	}

	@Override
	public Page getPageByHql(Page page) {
		Searchable searchable = new HqlSearchable();
		searchable.table(Sample.class, "s");
		searchable.select("s.name as name");
		searchable.select("s.age as age");
		searchable.prefixLike("s.name", "张三");
		searchable.or(new OrCondition("s.name", SearchOperator.eq, "张三"), new OrCondition("s.name", SearchOperator.like, "张三"),new OrCondition("s.name", SearchOperator.eq, "张三1"));
		searchable.ascSort("s.age");
		
		searchable.setPage(page);
		QueryBean q = new QueryBean();
		System.out.println(q.findBySearchParams(searchable));
		List<SampleVo> list = sampleRepository.findALLForSpecifyType(searchable, SampleVo.class);
		page = sampleRepository.findAllForPage(searchable);
		if (!CollectionUtils.isEmpty(page.getRows())) {
			List<Sample> list1 = (List<Sample>) page.getRows();
			for (Sample sample : list1) {
				System.out.println(sample.getName() + " " + sample.getAge() + " " + sample.getNickName());
			}
		}
		return page;
	}

	@Override
	public Page getPageBySql(Page page) {
		Searchable searchable = new SqlSearchable("t_Sample", "s");
//		searchable.join("t_Sample", "a"); //添加关联表
		searchable.select("s.name").select("s.nickName,s.age");
		searchable.gte("s.age", 20);
		searchable.descSort("s.age");
		searchable.setPage(page);
		List<SampleVo> list = sampleRepository.findALLForSpecifyType(searchable, SampleVo.class);
		page = sampleRepository.findAllForPage(searchable);
		if (!CollectionUtils.isEmpty(page.getRows())) {
			List<Sample> list1 = (List<Sample>) page.getRows();
			for (Sample sample : list1) {
				System.out.println(sample.getName() + " " + sample.getNickName() + " " + sample.getAge());
			}
		}
		return page;
	}
}
